import {
  deleteChargeCostApi,
  submitQuotationApi,
  flushSettlementPriceApi,
  updateUnitPriceApi,
  getCombinedWaybillsApi,
  mergeUnitPriceApi
} from "@/api/waybillManagement/businessWaybill/waybillQuotation";
import { Modal, message } from "ant-design-vue";
import { Ref, createVNode } from "vue";
import { ExclamationCircleOutlined } from "@ant-design/icons-vue";
export default function OperationHook(state: {
  drawerOpts: Base.DrawerOpts;
  passData: Ref<WaybillQuotation.ListRows>;
  dialogOpts: Base.DialogOpts;
  formState: Ref<any>;
  emit: any;
  init: (id: string) => void;
  initInfo: (id: string) => void;
  getAdjustUnitPrice: Ref<number>;
  addChargeFeeCallBack: () => void;
  offerStatus: Ref<number>;
  saveOpType: Ref<string>;
  loading: Ref<boolean>;
}) {
  const { drawerOpts, dialogOpts, passData, formState, init, initInfo, getAdjustUnitPrice, addChargeFeeCallBack, offerStatus, saveOpType, loading } = state;
  // 查看发票
  const showInvoiceHandle = () => {
    drawerOpts.width = "1200px";
    drawerOpts.title = "查看发票";
    drawerOpts.type = 1;
    drawerOpts.passData = { id: passData.value.shipmentId, ...passData.value };
    drawerOpts.component = "ShowInvoice";
    drawerOpts.visible = true;
  };
  // 新增附加费
  const addChargeHandle = () => {
    dialogOpts.width = "520px";
    dialogOpts.title = "添加附加费";
    dialogOpts.type = 1;
    dialogOpts.passData = { waybillNum: passData.value.waybillNum };
    dialogOpts.component = "AddChargeFee";
    dialogOpts.visible = true;
  };

  // 修改调整单价
  const changeAdjustUnitPriceHandle = (e: string) => {
    saveOpType.value = e;
    if (formState.value.freightCost.opType === "1") {
      formState.value.freightCost.clearingUnitPrice = (((formState.value.freightCost.unitPrice + formState.value.freightCost.adjustUnitPrice) * 100) / 100).toFixed(2) || 0;
    } else {
      formState.value.freightCost.clearingUnitPrice = (((formState.value.freightCost.unitPrice - formState.value.freightCost.adjustUnitPrice) * 100) / 100).toFixed(2) || 0;
    }
  };
  //输入调整单价回调
  const savePriceHandle = (e: number) => {
    getAdjustUnitPrice.value = e;
    if (formState.value.freightCost.opType === "1") {
      formState.value.freightCost.clearingUnitPrice = (((formState.value.freightCost.unitPrice + formState.value.freightCost.adjustUnitPrice) * 100) / 100).toFixed(2) || 0;
    } else {
      formState.value.freightCost.clearingUnitPrice = (((formState.value.freightCost.unitPrice - formState.value.freightCost.adjustUnitPrice) * 100) / 100).toFixed(2) || 0;
    }
  };

  // 删除附加费
  const deleteChargeCostHandle = (row: Base.GeneraDataType) => {
    Modal.confirm({
      title: "是否删除当前附加费?",
      icon: createVNode(ExclamationCircleOutlined),
      onOk() {
        deleteChargeCostApi({ id: row.id }).then((res: any) => {
          const { code } = res;
          if (code === 200) {
            message.success("删除成功!");
            // if (passData.value.orderShipmentQuotationId) initInfo(passData.value.orderShipmentQuotationId as unknown as string);
            // else init(passData.value.shipmentId);
            addChargeFeeCallBack();
          }
        });
      }
    });
  };
  // 修改客户备注
  const modifyRemarkHandle = () => {
    dialogOpts.title = `修改客户备注`;
    dialogOpts.width = "520px";
    dialogOpts.type = 1;
    dialogOpts.component = "ModifyRemark";
    dialogOpts.passData = formState.value.bizPreOrderShipmentResDto;
    dialogOpts.visible = true;
  };
  // 修改客户备注
  const modifyInnerRemarkHandle = () => {
    dialogOpts.title = `修改内部备注`;
    dialogOpts.width = "520px";
    dialogOpts.type = 2;
    dialogOpts.component = "ModifyRemark";
    dialogOpts.passData = formState.value.bizPreOrderShipmentResDto;
    dialogOpts.visible = true;
  };
  // 查看材积明细
  const showBoxInfoHandle = () => {
    drawerOpts.width = "1200px";
    drawerOpts.title = "查看材积明细";
    drawerOpts.type = 1;
    drawerOpts.passData = passData.value;
    drawerOpts.component = "BoxInfo";
    drawerOpts.visible = true;
  };
  // 提交/重新报价
  const submitHandle = () => {
    const params = {
      id: passData.value.orderShipmentQuotationId ? passData.value.orderShipmentQuotationId : (null as unknown as string),
      shipmentId: passData.value.shipmentId,
      waybillNum: passData.value.waybillNum,
      baseUnitPrice: formState.value.freightCost.unitPrice || 0,
      adjustUnitPrice: formState.value.freightCost.opType === "1" ? formState.value.freightCost.adjustUnitPrice || 0 : -formState.value.freightCost.adjustUnitPrice || 0,
      adjustReason: formState.value.freightCost.adjustReason
    };
    loading.value = true;
    return submitQuotationApi(params)
      .then((res: any) => {
        const { code } = res;
        if (code === 200) {
          message.success("操作成功!");

          if (passData.value.orderShipmentQuotationId) {
            initInfo(passData.value.orderShipmentQuotationId);
          } else {
            init(passData.value.shipmentId);
          }

          // emit("submit");
          offerStatus.value = 2;
        }
      })
      .finally(() => {
        loading.value = false;
      });
  };
  // 修改运单单价
  const editUnitPriceHandle = () => {
    const params = {
      shipmentId: passData.value.shipmentId,
      unitPrice: formState.value.freightCost.unitPrice
    };
    return updateUnitPriceApi(params).then((res: any) => {
      const { code } = res;
      if (code === 200) {
        if (passData.value.orderShipmentQuotationId) {
          initInfo(passData.value.orderShipmentQuotationId);
        } else {
          init(passData.value.shipmentId);
        }
        message.success("操作成功!");
      }
    });
  };
  //更新单价
  const updateHandle = () => {
    loading.value = true;
    flushSettlementPriceApi({ id: passData.value.shipmentId })
      .then((res: any) => {
        const { code, data } = res;
        if (code === 200) {
          formState.value.freightCost.unitPrice = data;
          if (passData.value.orderShipmentQuotationId) {
            initInfo(passData.value.orderShipmentQuotationId);
          } else {
            init(passData.value.shipmentId);
          }
          message.success("操作成功!");
        }
      })
      .finally(() => {
        loading.value = false;
      });
  };
  // 编辑附加费
  const editChargeHandle = (row: Base.GeneraDataType) => {
    dialogOpts.width = "520px";
    dialogOpts.title = "编辑附加费";
    dialogOpts.type = 2;
    dialogOpts.passData = { waybillNum: passData.value.waybillNum, ...row };
    dialogOpts.component = "AddChargeFee";
    dialogOpts.visible = true;
  };
  // 合并报关详情
  const cancelMergeDeclarationHandle = () => {
    dialogOpts.title = `取消合并报关`;
    dialogOpts.width = "500px";
    dialogOpts.type = 3;
    dialogOpts.component = "CustomsClearance";
    dialogOpts.passData = [passData.value];
    dialogOpts.visible = true;
  };
  //合并单价
  const mergeUnitPriceHandle = (row: any) => {
    loading.value = true;

    mergeUnitPriceApi({ id: row.id })
      .then((res: any) => {
        const { code } = res;
        if (code === 200) {
          message.success("操作成功!");
          addChargeFeeCallBack();
        }
      })
      .finally(() => {
        loading.value = false;
      });
  };

  return {
    showInvoiceHandle,
    addChargeHandle,
    changeAdjustUnitPriceHandle,
    deleteChargeCostHandle,
    modifyRemarkHandle,
    modifyInnerRemarkHandle,
    showBoxInfoHandle,
    submitHandle,
    updateHandle,
    editUnitPriceHandle,
    savePriceHandle,
    editChargeHandle,
    cancelMergeDeclarationHandle,
    mergeUnitPriceHandle
  };
}
